﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using DTO;
using BUS;
namespace QuanLyThuVien
{
    public partial class TheDocGia : Form
    {
        public TheDocGia()
        {
            InitializeComponent();
        }
        int UserID;
        int MaDocGia = -1;
        void LoadDanhSachLoaiDocGia()
        {
            LoaiDocGiaBUS loaiDocGiaBUS=new LoaiDocGiaBUS();
            List<LoaiDocGiaDTO> loaiDocGiaDTO = loaiDocGiaBUS.LayDanhSachLoaiDocGia_BUS();
            cbbloaidocgia.DataSource = loaiDocGiaDTO;
            cbbloaidocgia.DisplayMember = "TenLoaiDocGia";
            cbbloaidocgia.ValueMember = "MaLoaiDocGia";
           
        }
        void LoadDanhSachTheDocGia()
        {
            TheDocGiaBUS theDocGiaBUS = new TheDocGiaBUS();
            DataTable dt = theDocGiaBUS.LayDanhSachTheDocGia_BUS();
            dgvDSDocGia.DataSource = dt;
            dgvDSDocGia.Columns["ID"].HeaderText = "Mã đọc giả";
            dgvDSDocGia.Columns["HoTen"].HeaderText = "Họ tên";
            dgvDSDocGia.Columns["LoaiDocGia"].HeaderText = "Loại đọc giả";
            dgvDSDocGia.Columns["NgaySinh"].HeaderText = "Ngày sinh";
            dgvDSDocGia.Columns["DiaChi"].HeaderText = "Địa chỉ";
            dgvDSDocGia.Columns["Email"].HeaderText = "Email";
            dgvDSDocGia.Columns["NgayLapThe"].HeaderText = "Ngày lập thẻ";
            dgvDSDocGia.Columns["NguoiLap"].HeaderText = "Người lập";
            dgvDSDocGia.Columns["MLDG"].Visible = false;
          

        }
        public bool IsValidEmail(string email)
        {
            try
            {
                int nFirt = int.Parse(email.Substring(0, 1));
                return false;
            }
            catch
            {
                string pattern = @"^[_a-zA-Z0-9][_.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[_.a-zA-Z0-9]+)*\.
    (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|vn|[a-zA-Z]{2})$";

                Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);

                bool valid = false;


                if (string.IsNullOrEmpty(email))
                {
                    valid = false;
                }
                else
                {

                    valid = check.IsMatch(email.ToLower());
                }

                return valid;
            }
        }
        bool  SetError()
        { 
            ErrorProvider pro = new ErrorProvider();
            if (txthocvaten.Text == "")
            {
               
                pro.SetError(txthocvaten, "Không được bỏ trống");
                return false;
            }
            if (IsValidEmail(txtemail.Text) == false)
            {
                pro.SetError(txtemail, "Email không hợp lệ");
                return false;
            }
            return true;
        }
        private void TheDocGia_Load(object sender, EventArgs e)
        {
            UserID = 22;
            LoadDanhSachLoaiDocGia();
            LoadDanhSachTheDocGia();
        }

        private void btlammoi_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupPanel1.Controls)
            {
               if(ctrl.GetType()!=typeof(DevComponents.DotNetBar.ButtonX) && ctrl.GetType()!=typeof(DevComponents.DotNetBar.LabelX))
               {
                   ctrl.ResetText();
               }
            }
        }

        private void btlapthe_Click(object sender, EventArgs e)
        {
            if (SetError() == false)
                return;
            TheDocGiaDTO theDocGiaDTO = new TheDocGiaDTO();
           
            theDocGiaDTO.Hoten = txthocvaten.Text;
            theDocGiaDTO.Diachi = txtdiachi.Text;
            theDocGiaDTO.Email = txtemail.Text;
            theDocGiaDTO.Nguoilap = UserID;
            theDocGiaDTO.Ngaylapthe = dtpNgayLapThe.Value;
            theDocGiaDTO.Ngaysinh = dtpNgaySinh.Value;
            theDocGiaDTO.Loaidocgia = int.Parse(cbbloaidocgia.SelectedValue.ToString());
            TheDocGiaBUS theDocGiaBUS = new TheDocGiaBUS();
           
                if (theDocGiaBUS.ThemTheDocGia_BUS(theDocGiaDTO))
                {
                    MessageBox.Show("Them the doc gia thanh cong", "Thong bao");
                    LoadDanhSachTheDocGia();
                }
                else
                    MessageBox.Show("Them the doc gia that bai", "Thong bao");
         
        }

        private void dgvDSDocGia_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDSDocGia.SelectedRows.Count > 0)
            {
                if (dgvDSDocGia.SelectedRows[0].IsNewRow == false)
                {
                    DataGridViewRow row = dgvDSDocGia.SelectedRows[0];
                    txtdiachi.Text = row.Cells["DiaChi"].Value.ToString();
                    txthocvaten.Text = row.Cells["HoTen"].Value.ToString();
                    txtnguoilap.Text = UserID.ToString();
                    txtemail.Text = row.Cells["Email"].Value.ToString();
                    cbbloaidocgia.SelectedValue = int.Parse(row.Cells["MLDG"].Value.ToString());
                    dtpNgayLapThe.Value = DateTime.Parse(row.Cells["NgayLapThe"].Value.ToString());
                    dtpNgaySinh.Value = DateTime.Parse(row.Cells["NgaySinh"].Value.ToString());
                    MaDocGia = int.Parse(row.Cells["ID"].Value.ToString());
                }
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (!SetError())
                return;
            TheDocGiaDTO theDocGiaDTO = new TheDocGiaDTO();
            theDocGiaDTO.Madocgia = MaDocGia;
            theDocGiaDTO.Hoten = txthocvaten.Text;
            theDocGiaDTO.Diachi = txtdiachi.Text;
            theDocGiaDTO.Email = txtemail.Text;
            theDocGiaDTO.Nguoilap = UserID;
            theDocGiaDTO.Ngaylapthe = dtpNgayLapThe.Value;
            theDocGiaDTO.Ngaysinh = dtpNgaySinh.Value;
            theDocGiaDTO.Loaidocgia = int.Parse(cbbloaidocgia.SelectedValue.ToString());
            if (MaDocGia != -1)
            {
                TheDocGiaBUS.CapNhatTheDocGia_BUS(theDocGiaDTO);
                LoadDanhSachTheDocGia();
            }
            else
                MessageBox.Show("Lập thẻ thất bại", "Thông báo");
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (MaDocGia != -1)
            {
                TheDocGiaBUS.XoaTheDocGia_BUS(MaDocGia);
                LoadDanhSachTheDocGia();
            }
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bttimthe_Click(object sender, EventArgs e)
        {
            DataTable dt = TheDocGiaBUS.TimTheDocGiaTheoTen_BUS(txthocvaten.Text);
            if (dt.Rows.Count > 0)
            {
                dgvDSDocGia.DataSource = dt;
                dgvDSDocGia.Columns["ID"].HeaderText = "Mã đọc giả";
                dgvDSDocGia.Columns["HoTen"].HeaderText = "Họ tên";
                dgvDSDocGia.Columns["LoaiDocGia"].HeaderText = "Loại đọc giả";
                dgvDSDocGia.Columns["NgaySinh"].HeaderText = "Ngày sinh";
                dgvDSDocGia.Columns["DiaChi"].HeaderText = "Địa chỉ";
                dgvDSDocGia.Columns["Email"].HeaderText = "Email";
                dgvDSDocGia.Columns["NgayLapThe"].HeaderText = "Ngày lập thẻ";
                dgvDSDocGia.Columns["NguoiLap"].HeaderText = "Người lập";
                dgvDSDocGia.Columns["MLDG"].Visible = false;
            }
            else
            {
                MessageBox.Show("Không tìm thấy thẻ đọc giả nào", "Thông báo");
            }
        }

        

       
    }
}